<?php

/* 
 * The include path may change 
 * depending on PHPCodeProvider.php location. 
 */

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 * It is possible to change the bracing style
 * and also to omit blank lines between the lines.
 */

$options = new CodeGeneratorOptions();

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromStatement call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the PHPCodeProvider class
 */

$provider = new PHPCodeProvider();

/*
 * Create a ThrowStatement instance.
 * The exception type is not passed to the constructor
 * so an Exception instance is generated.
 */

$throwStatement1 = new ThrowStatement();

/*
 * $throwStatement1 is an ObjectCreateExpression instance
 * so we can add the exception message as the first parameter to the constructor
 */

$throwStatement1->ToThrow->Parameters->add(
    new PrimitiveExpression('Exception thrown!'));

/*
 * Generate the code
 */

$provider->generateCodeFromStatement($throwStatement1, $writer, $options);

/*
 * Throw an InvalidArgumentException
 */

$throwStatement2 = new ThrowStatement();
$throwStatement2->ToThrow = new ObjectCreateExpression(
    new TypeReference('InvalidArgumentException'));
$throwStatement2->ToThrow->Parameters->add(
    new PrimitiveExpression('The value should be a string.'));

/*
 * Generate the code
 */

$provider->generateCodeFromStatement($throwStatement2, $writer, $options);

/*
 * Output the generated code using $writer->get_String();
 */

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) .
    '<pre/>';

/*
 * The output should be:
 * 
 * throw new Exception('Exception thrown!');
 * throw new InvalidArgumentException('The value should be a string.');
 * 
 */

?>
